package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.common.entity.PageResult;
import com.itheima.common.entity.QueryPageBean;
import com.itheima.mapper.SetmealMapper;
import com.itheima.pojo.Setmeal;
import com.itheima.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    SetmealMapper setmealMapper;

    /**
     * 新增
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void addSetmeal(Setmeal setmeal, List<Integer> checkgroupIds) {
        setmealMapper.addSetmeal(setmeal);
        setmealMapper.addSetmealAndCheckGroup(setmeal.getCode(), checkgroupIds);
    }

    /**
     * 删除
     * @param id
     */
    @Override
    public void deleteById(Integer id) {
        setmealMapper.deleteSetmealAndCheckGroup(id);
        setmealMapper.deleteById(id);
    }

    /**
     * 修改
     * @param setmeal
     * @param checkgroupIds
     */
    @Override
    public void edit(Setmeal setmeal, List<Integer> checkgroupIds) {
        setmealMapper.deleteSetmealAndCheckGroup(setmeal.getId());
        setmealMapper.deleteById(setmeal.getId());
        setmealMapper.addSetmeal(setmeal);
        setmealMapper.addSetmealAndCheckGroup(setmeal.getCode(), checkgroupIds);
    }

    /**
     * 根据id查询套餐信息
     * @param id
     * @return
     */
    @Override
    public Setmeal findById(Integer id) {
        return setmealMapper.findById(id);
    }

    @Override
    public PageResult findPage(QueryPageBean queryPageBean) {
        Integer currentPage = queryPageBean.getCurrentPage();
        Integer pageSize = queryPageBean.getPageSize();
        String queryString = queryPageBean.getQueryString();
        PageHelper.startPage(currentPage, pageSize);
        List<Setmeal> list = setmealMapper.pageList(currentPage, pageSize, queryString);
        PageInfo<Setmeal> pageInfo = new PageInfo<>(list);
        return new PageResult(pageInfo.getTotal(), pageInfo.getList());
    }
}
